package receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.ixintui.pushsdk.SdkConstants;

import io.github.wjehovah.yyn.activity.WebShowActivity;

public class PushReceiver extends BroadcastReceiver {

    static final String TAG = "ixintui_demo";

    static String  openWebUrl;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        
        if (action.equals(SdkConstants.MESSAGE_ACTION)) {
            String msg = intent.getStringExtra(SdkConstants.MESSAGE);
            String extra = intent.getStringExtra(SdkConstants.ADDITION);
            
            Log.e(TAG, "message received, msg is: " + msg + "extra: " + extra);

            openWebUrl =  msg;

        } 
        else if (action.equals(SdkConstants.RESULT_ACTION)) {
            String cmd = intent.getStringExtra(SdkConstants.COMMAND);
            int code = intent.getIntExtra(SdkConstants.CODE, 0);
            if (code != 0) {
                String error = intent.getStringExtra(SdkConstants.ERROR);
                Log.d(TAG, "command is: " + cmd + " result error: " + error);
            } else {
                Log.d(TAG, "command is: " + cmd + "result OK");
            }
            
            String extra = intent.getStringExtra(SdkConstants.ADDITION);
            if (extra != null) {
                Log.d(TAG, "result extra: " + extra);
            }
            

        }
        else if (action.equals(SdkConstants.NOTIFICATION_CLICK_ACTION)) {
            String msg = intent.getStringExtra(SdkConstants.MESSAGE);
           //Log.d(TAG, "notification click received, msg is: " + msg);
            if(!TextUtils.isEmpty(openWebUrl)){
                Log.e("TAG",openWebUrl);
                Intent intent2 = new Intent(context, WebShowActivity.class);
                intent2.putExtra("WEBURL",openWebUrl);
                intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent2);
            }
        }
    }
    

    
}
